﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace _2C.Framework.BaseUtility.Helper
{    /// <summary>
     /// XML与对象转换类//
     /// </summary>
    public static class XmlHelper
    {
        public static T Load<T>(string filePath) where T : class
        {
            XmlSerializer sz = new XmlSerializer(typeof(T));
            if (File.Exists(filePath))
            {
                using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    T obj = (T)sz.Deserialize(fs);
                    return obj;
                }
            }
            return null;
        }

        public static void Save<T>(T obj, string filePath) where T : class
        {
            XmlSerializer sz = new XmlSerializer(typeof(T));
            using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite))
            {
                sz.Serialize(fs, obj);
            }
        }
    }
}